
package controller.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import model.domain.Role;

public class RoleConverter implements Converter {

    public RoleConverter() {
    }


    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {

        if (value == null || (value.trim().length() == 0))
        {
            return value;
        }

        Role role = new Role();

        String[] splitedString = value.split(" - ");
        role.setRoleID(Integer.parseInt(splitedString[0]));
        role.setDescription(splitedString[1]);

        return role;
    }

    @Override
      public String getAsString(FacesContext context, UIComponent component, Object value)
      {

       Role role = null;

        if (value instanceof Role)
        {
            role = (Role)value;

            StringBuilder roleAsString = new StringBuilder();
            roleAsString.append(role.getRoleID());
            roleAsString.append(" - ");
            roleAsString.append(role.getDescription());
            String string = roleAsString.toString();

            return string;
        }
        return "";
    }
}
